package com.wanxing.sellerapp.audio

import android.content.Context
import android.media.AudioAttributes
import android.media.AudioManager
import android.media.SoundPool
import com.wanxing.sellerapp.App
import com.wanxing.sellerapp.R

object AudioHelper {

    var  volume = 1F
    init{
        App.configModel.volume.observeForever{ volume = it }
    }

//    /**
//     * 开1号门语音
//     */
//    const val SOUND_DOOR_OPEN_1 = 11
    /**
     *  调试开门成功
     */
    const val SOUND_DOOR_OPEN_SUC = 15
    /**
     * 调试开门失败
     */
    const val SOUND_DOOR_OPEN_FAIL = 16
    /**
     * 关门
     */
    const val SOUND_DOOR_CLOSE = 2

    /**
     *  调试关门成功
     */
    const val SOUND_DOOR_CLOSE_SUC = 71
    /**
     * 调试关门失败
     */
    const val SOUND_DOOR_CLOSE_FAIL = 72

    /**
     * 系统错误
     */
    const val SOUND_SYS_ERROR = 50
    /**
     * 设备使用者
     */
    const val SOUND_DEVICE_USING = 51
    /**
     * 设备故障
     */
    const val SOUND_DEVICE_ERROR = 52

    /**
     * 网络异常
     */
    const val SOUND_NET_ERROR = 54

    /**
     * 设备未注册
     */
    const val SOUND_D_NOT_REG = 55

    /**
     * 设备未启用
     */
    const val SOUND_D_NOT_USE = 56

    /**
     * 余额不足
     */
    const val SOUND_NO_BALANCE = 57

    /**
     * 卡已过期
     */
    const val SOUND_CARD_EXPIRE = 58

    /**
     * 卡号不存在
     */
    const val SOUND_CARD_INVALIDATE = 59
    /**
     * 卡已冻结 （挂失或冻结）
     */
    const val SOUND_CARD_FROZEN = 510

    /**
     * 卡已失效
     */
    const val SOUND_CARD_SX = 511


    private val mSoundPool = SoundPool
        .Builder()
        .setMaxStreams(30)
        .setAudioAttributes(
            AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                .setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN)
                .build()
        ).build()

    /**
     * 11 - 开1门
     * 12 - 开2门
     * 13 - 开3门
     * 14 - 开4门
     * 2 - 关门
     * 3 - 校准成功
     * 4 - 校准失败
     * 5 - 清零成功
     * 6 - 清零失败
     */
    private val soundId = mutableMapOf<Int, Int>()

    fun init(context: Context) {
//        soundId[SOUND_DOOR_OPEN_1] = mSoundPool.load(context, R.raw.door_open_1, 1)
        soundId[SOUND_DOOR_CLOSE] = mSoundPool.load(context, R.raw.door_close, 1)
        soundId[SOUND_DOOR_OPEN_SUC] = mSoundPool.load(context, R.raw.open_door_suc, 1)
        soundId[SOUND_DOOR_OPEN_FAIL] = mSoundPool.load(context, R.raw.open_door_fail, 1)
        soundId[SOUND_DOOR_CLOSE_SUC] = mSoundPool.load(context, R.raw.close_door_suc, 1)
        soundId[SOUND_DOOR_CLOSE_FAIL] = mSoundPool.load(context, R.raw.close_door_fail, 1)
        soundId[SOUND_SYS_ERROR] = mSoundPool.load(context, R.raw.sys_error, 0  )
        soundId[SOUND_DEVICE_ERROR] = mSoundPool.load(context, R.raw.device_error, 0  )
        soundId[SOUND_DEVICE_USING] = mSoundPool.load(context, R.raw.device_using, 0  )
        soundId[SOUND_NET_ERROR] = mSoundPool.load(context, R.raw.net_error, 0  )
        soundId[SOUND_CARD_FROZEN] = mSoundPool.load(context, R.raw.card_pause, 0  )
        soundId[SOUND_CARD_EXPIRE] = mSoundPool.load(context, R.raw.card_expire, 0  )
        soundId[SOUND_CARD_SX] = mSoundPool.load(context, R.raw.card_sx, 0  )
        soundId[SOUND_CARD_INVALIDATE] = mSoundPool.load(context, R.raw.card_invidate, 0  )
        soundId[SOUND_D_NOT_USE] = mSoundPool.load(context, R.raw.d_not_use, 0  )
        soundId[SOUND_NO_BALANCE] = mSoundPool.load(context, R.raw.no_balance, 0  )
    }

    /**
     * @see AudioHelper.SOUND_DOOR_OPEN_1
     * @see AudioHelper.SOUND_DOOR_CLOSE
     * @param sound
     */
    fun play(sound: Int){
        soundId[sound]?.let { id ->
            mSoundPool.play(id, volume, volume, 0, 0, 1F)
        }
    }

    fun playDoorOpen(doorNo: Int){
        soundId[10 + doorNo]?.let { id ->
            mSoundPool.play(id, volume, volume, 0, 0, 1F)
        }
    }

    fun release(){
        mSoundPool.release()
    }

}